﻿using System;
using System.Collections.Generic;
using System.Text;
using taxi_sovereign.interfaces;

namespace taxi_sovereign.model
{
    [Serializable]
    class Route:IRoute
    {
        private IRoad[] RoadRoute = new IRoad[0];
        private IStop[] StopRoute = new IStop[0];

        #region IRoute Members

        public Route(IRoad[] RoadsToAdd, IStop[] StopsToAdd)//Создать маршрут
        {
            RoadRoute = RoadsToAdd;
            StopRoute = StopsToAdd;
            //RoadsToAdd.CopyTo(RoadRoute, 0);
            //StopsToAdd.CopyTo(StopRoute, 0);
        }

        public void Rebuild()
        {
            IStop[] temps = new IStop[0];
            foreach (IRoad r in RoadRoute)
            {
                Array.Resize(ref temps, temps.Length + r.GetStopArray(r.GetFirstCross()).Length);
                IStop[] addition = r.GetStopArray(r.GetFirstCross());
                for (int i = 0; i < addition.Length; i++)
                {
                    temps[i + temps.Length - addition.Length] = addition[i];
                }
            }
        }

        public bool StopAvailable(IStop CurrentStop, IStop NeededStop, int direct)
        {
            bool found1 = false, found2 = false;
            int i = (direct>0)?0:StopRoute.Length-1;
            while (!found2 && i<StopRoute.Length && i>=0)
            {
                if (CurrentStop == StopRoute[i]) found1 = true;
                if (NeededStop == StopRoute[i]) found2 = true;
                i+=direct;
            }
            if (!found1) return false;
            else
                if (found2 && found1) return true;
                else
                    return false;
        }

        public IStop[] GetStops()
        {
            return StopRoute;
        }
        
        //!!!!!!!!!Дублирует функционал конструктора
        /*public void SetRoute(IRoad[] roads, IStop[] stops)
        {
            RoadRoute = roads;
            StopRoute = stops;
        }*/

        public IRoad[] GetRoad()
        {
            return RoadRoute;
        }

        public void SetRoute(IRoad[] roads)
        {
            RoadRoute = roads;
        }

        public IRoad GetNextRoad(IRoad currroad, int direction)
        {
            
            if (direction == 1)
            {
                for (int i = 0; i < RoadRoute.Length - 1; i++)
                {
                    if (RoadRoute[i] == currroad)
                    {
                        
                        return RoadRoute[i + 1];
                        
                        
                    }
                }
            }
            else
            {
                for (int i = RoadRoute.Length - 1; i > 0; i--)
                {
                    if (RoadRoute[i] == currroad)
                    {
                        
                        return RoadRoute[i - 1];
                        
                        
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
